<?php
// +--------------------------------------------------------------------------
// | ZAIYOUDAO [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2016 载攸道 版权所有 <TEL:17090888854>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * Mall
 */
namespace Mall\Controller;
use Common\Controller\AdminController;

/**
 * 商品管理
 */
class GoodsController extends AdminController{
	
	/**
	 * 文章管理
	 */
    public function Goods(){

		common_public()->assign_page('商品管理');
		
		$m_mall_content = M('mall_content');
		$m_common_user = M('common_user');
		
		
		//判断是否搜索用户名
		$name = I('param.name');
		if($name){
			
			$map['name'] = array('like',"%$name%");

		}
		$this->assign('name',$name);
		

		//获得上级信息
		$form_id = I('param.form_id')?I('param.form_id'):0;
		
		
		//获得菜单信息
		if($form_id!=0){
			
			$map['form_id'] = $form_id;
		}
		
		//判断是否搜索用户名
		$status = I('param.status');
		if($status){
			
			$map['status'] = $status;

		}else{
			
			$map['status'] = array('in','1,2,3');
		
			
		}
		$this->assign('status',$status);
		
		//上传开始结束时间
		$down_money = I('param.down_money');
		$top_money = I('param.top_money');
		if($down_money != null and $top_money != null){
			
			$map['money']  = array(array('egt',$down_money),array('elt',$top_money),'and'); 
			
			$this->assign('down_money',$down_money);
			$this->assign('top_money',$top_money);
		}
		
		
		$map['type'] = get_mall_model_conf('MALL_GOODS');
		
		
		
		$list = $m_mall_content->where($map)->order('status asc,time desc')->page($_GET['p'].',50')->select();
	
		$count      = $m_mall_content->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		//查询关联信息
		foreach($list as $i=>$k){
			
			$fmap['id'] = $list[$i]['form_id'];
			$fdata = $m_mall_content->where($fmap)->find();
			$umap['id'] = $list[$i]['uid'];
			
			$udata = $m_common_user->where($umap)->find();
			
			
			$list[$i]['channel'] = $fdata['name'];
			$list[$i]['nickname'] = $udata['nickname'];
			$list[$i]['n_img'] = $udata['img'];
			
		
		}
		//赋值
		$this->assign('list',$list);
		$this->assign('data',$data);
		$this->assign('form_id',$fmap['id']);
		
		$classify = $this->getChannel();
		
		
		$this->assign('classify',$classify);
		$this->assign('classifys',$classify);
		
		
		$this->display();
	}
	
	/**
	 * 文章管理
	 */
    public function Manage(){

		common_public()->assign_page('商品管理');
		
		$m_mall_content = M('mall_content');
		$m_common_user = M('common_user');
		
		
		//判断是否搜索用户名
		$name = I('param.name');
		if($name){
			
			$map['name'] = array('like',"%$name%");

		}
		$this->assign('name',$name);
		

		//获得上级信息
		$form_id = I('param.form_id')?I('param.form_id'):0;
		
		
		//获得菜单信息
		if($form_id!=0){
			
			$map['form_id'] = $form_id;
		}
		
		//判断是否搜索用户名
		$status = I('param.status');
		if($status){
			
			$map['status'] = $status;

		}else{
			
			$map['status'] = array('in','1,2,3');
		
			
		}
		$this->assign('status',$status);
		
		//上传开始结束时间
		$down_money = I('param.down_money');
		$top_money = I('param.top_money');
		if($down_money != null and $top_money != null){
			
			$map['money']  = array(array('egt',$down_money),array('elt',$top_money),'and'); 
			
			$this->assign('down_money',$down_money);
			$this->assign('top_money',$top_money);
		}
		
		
		$map['type'] = get_mall_model_conf('MALL_GOODS');
		
		
		
		$list = $m_mall_content->where($map)->order('status asc,time desc')->page($_GET['p'].',50')->select();
	
		$count      = $m_mall_content->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		//查询关联信息
		foreach($list as $i=>$k){
			
			$fmap['id'] = $list[$i]['form_id'];
			$fdata = $m_mall_content->where($fmap)->find();
			$umap['id'] = $list[$i]['uid'];
			
			$udata = $m_common_user->where($umap)->find();
			
			
			$list[$i]['channel'] = $fdata['name'];
			$list[$i]['nickname'] = $udata['nickname'];
			$list[$i]['n_img'] = $udata['img'];
			
		
		}
		//赋值
		$this->assign('list',$list);
		$this->assign('data',$data);
		$this->assign('form_id',$fmap['id']);
		
		$classify = $this->getChannel();
		
		
		$this->assign('classify',$classify);
		$this->assign('classifys',$classify);
		
		
		$this->display();
	}
	
	/**
	 * 获得分类
	 */
	public function getChannel(){
		
			$m_mall_content = M('mall_content');
		
			//获得顶级分类
			$map['form_id'] = 0;
			$map['type'] = get_mall_model_conf('MALL_CLASSIFY');
			$map['status'] = 1;
			
			$list = $m_mall_content->where($map)->order('sort desc')->select();
			
			//获得下级分类即商品数量
			foreach($list as $i=>$k){
				
				$down_map['form_id'] = $list[$i]['id'];
				$down_map['type'] = get_mall_model_conf('MALL_CLASSIFY');
				$down_map['status']  =1;
				$down_list = $m_mall_content->where($down_map)->order('sort desc')->select();
				
				$list[$i]['down'] = $down_list;
				
			}
		
	
			return $list;
	}
	
	/**
	 * 新增文章
	 */
	public function Create(){
		

		$m_mall_content = M('mall_content');
			
		//判断是否是ajax请求
		if(IS_POST){

			
			//获得数据
			$data = I();
			
			$data['value'] = I('param.value','','');
			$data['uid'] = getUID();
			$data['form_id'] =  I('param.form_id')?I('param.form_id'):0;;
			$data['type'] = get_mall_model_conf('MALL_GOODS');;
			$data['time'] = time();
			$data['ip'] = get_client_ip();
			$data['status'] = 1;
			
			$result = $m_mall_content->add($data);
			
			if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER'] 
			
				$this->success('新增成功', U('Mall/Goods/Manage'));
			
			} else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置  
			
				$this->error('新增失败');
			
			}
		
				
		}else{
			
			
				$classify = $this->getChannel();
		
			
	
				$this->assign('list',$classify);
		
				$this->display();
		}
		
	
	}
	
	/**
	 * 文章详情
	 */
	public function Detail(){
    
		$m_mall_content = M('mall_content');
		
		common_public()->assign_page('页面详情');
		
		$map['id'] = I('param.id');
		$map['type'] = get_mall_model_conf('MALL_GOODS');
		$map['status'] = array('in','1,2,3');
			
		$data = $m_mall_content->where($map)->find();
			
		$this->assign('data',$data);
		
			
		$this->display();
		
		
	
	}
	/**
	 * 编辑文章
	 */
	public function Edit(){
    
		$m_mall_content = M('mall_content');
		
		$map['id'] = I('param.id');
		//判断是否是ajax请求
		if(IS_POST){

			
			set_mall_draft($map['id']); //保存草稿
			
			$data = I('');
			unset($data['id']);
			$data['value'] = I('param.value','','');
			$data['time'] = time();
			
			$data['ip'] = get_client_ip();
			
			$result = $m_mall_content->where($map)->save($data);
			
			if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER'] 
			
				$this->success('编辑成功', U('Mall/Goods/Manage'));
			
			} else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置  
			
				$this->error('编辑失败');
			
			}
		
		}else{
			
			$map['type'] = get_mall_model_conf('MALL_GOODS');
			$map['status'] = array('in','1,2,3');
		
		
			$data = $m_mall_content->where($map)->find();
			
			$this->assign('data',$data);
		
			$classify = $this->getChannel();
		

			$this->assign('list',$classify);
				
			$this->display();
		}
		
	
	}
	
	/**
	 * 批量改变状态
	 */
	public function Delete($id,$status=0){
		
	
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_mall_content = M('mall_content');
		
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			
			if($id){
				
			
				$id = explode(',',$id); 
				
				
				foreach($id as $i=>$k){
					
					
					$map['id'] = $id[$i];
					$data['status'] = $status;
					
					$m_mall_content->where($map)->save($data);
					
				}
				
				//返回信息
				$info = get_errcode(0);
			
			}else{
				
				$info = get_errcode(10000001);
			}
			
	
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);


	}
	
	
}